#include "gas.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include <stdio.h>
#include "cJSON.h"

// 获取可燃气体检测数据
static void *Sensor_TASK(void)
{

    uint16_t gas_value = 0;
    char json_buffer[128];
    // while (1)
    // {
    //     gas_value=gas_get_data();
    //     printf("gas_value：%d%%\n", gas_value);
    //    osDelay(200);
    // }
    while (1)
    {
        gas_get_json_data(json_buffer, sizeof(json_buffer));
        printf("%s\n", json_buffer);
        osDelay(200);
    }
    //     while (1)
    // {
    //     gas_value=gas_get_data();
    //     cJSON *root = cJSON_CreateObject();
    //     // 根对象
    //     cJSON *servicesArray = cJSON_CreateArray();          // services 数组
    //     cJSON *serviceObject = cJSON_CreateObject();         // 单个 service 对象
    //     cJSON *propertiesObject = cJSON_CreateObject();      // properties 对象

    //     // 填充数据
    //     cJSON_AddStringToObject(serviceObject, "service_id", "Test_ID");                // 添加 service_id
    //     cJSON_AddNumberToObject(propertiesObject, "gas_value", gas_value);
    //     cJSON_AddItemToObject(serviceObject, "properties", propertiesObject);
    //     cJSON_AddItemToArray(servicesArray, serviceObject);                             // 将 service 加入数组
    //     cJSON_AddItemToObject(root, "services", servicesArray);                         // 将数组加入根对象           // 将 properties 加入 service
    //     char *jsonData = cJSONPrint(root); 
    //     printf("%s\n", jsonData);
    //     cJSON_Delete(root);
    //     // free(jsonData);
    //     osDelay(200);
    // }
}

static void MAIN_Entry(void)
{
    /********sensor init*******/
    // GAS init
gas_init();

    osThreadAttr_t attr;
    attr.name = "Sensor_TASK";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 5;
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)Sensor_TASK, NULL, &attr) == NULL)
    {
        printf("[MAIN_Entry] create Sensor_TASK NG\n");
    }
    else
    {
        printf("[MAIN_Entry] create Sensor_TASK OK\n");
    }

}
APP_FEATURE_INIT(MAIN_Entry);